﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace 数据的导入导出_经常用_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*  if (ofdImport.ShowDialog() == DialogResult.OK)
              {
                  using (FileStream fileStream = File.OpenRead(ofdImport.FileName))
                  {
                      using (StreamReader streamReader = new StreamReader(fileStream))
                      {
                          string line = null;
                          while ((line = streamReader.ReadLine()) != null)
                          {
                              string[] strs = line.Split('|');
                              string name = strs[0];
                              int age = Convert.ToInt32(strs[1]);
                              string SqlStr = @"Data Source=.; Initial Catalog=Mydb_1;User ID=sa;Password=13817995048";
                              using (SqlConnection conn = new SqlConnection(SqlStr))
                              {
                                  conn.Open();
                                  using (SqlCommand cmd = conn.CreateCommand())
                                  {
                                      cmd.CommandText = "insert into T_Vip(FName,FAge) values(@Name,@Age)";
                                      cmd.Parameters.Add(new SqlParameter("Name", name));
                                      cmd.Parameters.Add(new SqlParameter("Age", age));
                                      cmd.ExecuteNonQuery();
                                  }
                              }
                          }
                      }
                  }
              }
              MessageBox.Show("导入成功！");
          }*/

            /////////////////////////////////////////////////改进版本///////////////////////////////////////////////////////

            if (ofdImport.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            using (FileStream fileStream = File.OpenRead(ofdImport.FileName))
            {
                using (StreamReader streamReader = new StreamReader(fileStream))
                {
                    string line = null;
                    string SqlStr = @"Data Source=.; Initial Catalog=Mydb_1;User ID=sa;Password=13817995048";
                    //创建链接到数据库的链接非常耗时，所以尽量不要每次都创建链接，否则会消耗很多时间的
                    using (SqlConnection conn = new SqlConnection(SqlStr))
                    {
                        conn.Open();
                        using (SqlCommand cmd = conn.CreateCommand())
                        {
                            cmd.CommandText = "insert into T_Vip(FName,FAge) values(@Name,@Age)";
                            while ((line = streamReader.ReadLine()) != null)
                            {
                                string[] strs = line.Split('|');
                                string name = strs[0];
                                int age = Convert.ToInt32(strs[1]);
                                cmd.Parameters.Clear();//!!!!!!!!!!!!!!!!一定不能少,在while中一直用的就是一个SqlParameter对象
                                cmd.Parameters.Add(new SqlParameter("Name", name));
                                cmd.Parameters.Add(new SqlParameter("Age", age));
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }

                }
            }
            MessageBox.Show("插入成功！");
        }
    }
}
